<?php

namespace app\admin\controller;

use app\common\controller\Base;
use \app\admin\model\Admin;
use think\facade\Request;

class AdminLogin extends Base
{
    public function adminLand()
    {
        // $this->ok('aaa');
        //获取参数
        $param = Request::param();
        $rule = [
            'username|用户名' => 'require',
            'password|密码' => 'require',
        ];
        //错误信息
        $msg = [
            'username' => '用户名不能为空',
            'password' => '密码不能为空',
        ];
        $validate = new \think\Validate($rule, $msg);
        if (!$validate->check($param)) {
            $err = $validate->getError();
            $this->fail($err);
        }
        //根据用户名和密码，查询管理员是否存在
        $info = Admin::where('username', $param['username'])->where('password', $param['password'])->find();
        //如果错误，给提示
        if (!$info) {
            $this->fail('用户名或密码错误');
        }
        //如果成功，返回数据
        $res = [
            'id' => $info['id'],
            'username' => $info['username'],
        ];
        $this->ok($res);
    }
}
